﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：（新产品开发）高温负载寿命10PCS
    /// </summary>
    public class R026 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;

            for (int i = 0; i < 1; i++)
            {
                try
                {
                    sheet.GetRow(2).GetCell(headColumnsIndex + 3).SetCellValue(head.CustomerCode);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 7).SetCellValue(head.Customer);

                    sheet.GetRow(3).GetCell(headColumnsIndex + 3).SetCellValue(head.LotNo);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 7).SetCellValue(head.TestTotalTime);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 11).SetCellValue(head.Size);
                    if(head.RippleCurrent>0)
                    sheet.GetRow(3).GetCell(headColumnsIndex + 14).SetCellValue(head.RippleCurrent);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 17).SetCellValue(head.TestFreq);
                 
                    sheet.GetRow(3).GetCell(headColumnsIndex + 20).SetCellValue(head.FoilP);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 24).SetCellValue(head.FoilN);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 26).SetCellValue(head.Eps);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 30).SetCellValue(head.LeadWire);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 34).SetCellValue(head.Rubber);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 36).SetCellValue(head.AlCase);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 39).SetCellValue(head.Paste);
                    //sheet.GetRow(3).GetCell(headColumnsIndex + 29).SetCellValue(head.DateCode);


                    sheet.GetRow(4).GetCell(headColumnsIndex + 3).SetCellValue(head.TestVoltage);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 7).SetCellValue(head.TestTemp);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 11).SetCellValue(head.Series);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 14).SetCellValue(head.Cap);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 17).SetCellValue(head.Voltage);
                    



                    if (head.RippleCurrent > 0)
                    {
                        sheet.GetRow(1).GetCell(headColumnsIndex + 0).SetCellValue("高 温 负 载 (RC) 寿 命 实 验 报 告");
                        
                        //RC:		mA/	120Hz
                        //if (sheet.GetRow(4).GetCell(headColumnsIndex + 7) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 7);
                        //sheet.GetRow(4).GetCell(headColumnsIndex + 7).SetCellValue("RC:");
                        //if (sheet.GetRow(4).GetCell(headColumnsIndex + 8) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 8);
                        //sheet.GetRow(4).GetCell(headColumnsIndex + 8).SetCellValue(head.RippleCurrent);
                        //if (sheet.GetRow(4).GetCell(headColumnsIndex + 9) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 9);
                        //sheet.GetRow(4).GetCell(headColumnsIndex + 9).SetCellValue("mA /");
                        //if (sheet.GetRow(4).GetCell(headColumnsIndex + 10) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 10);
                        //sheet.GetRow(4).GetCell(headColumnsIndex + 10).SetCellValue(head.TestFreq);

                        ////Load  life (DC+RC) Test Report
                        //sheet.GetRow(1).GetCell(headColumnsIndex + 0).SetCellValue("Load  life (DC+RC) Test Report");
                    }

                    //sheet.GetRow(5).GetCell(headColumnsIndex + 52).SetCellValue(head.SampleDate.ToString("yyyy/M/d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 50).SetCellValue(head.TestDate.ToString("yyyy/M/d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 51).SetCellValue(head.TestDate.ToString("yyyy/M/d"));



                    sheet.GetRow(76).GetCell(headColumnsIndex + 6).SetCellValue(head.Remark);

                    //MAX:+25%  MIN:-25%
                    if (sheet.GetRow(76) == null) sheet.CreateRow(76);
                    if (sheet.GetRow(76).GetCell(3) == null) sheet.GetRow(76).CreateCell(3);
                    sheet.GetRow(76).GetCell(3).SetCellValue(
                        string.Format("MAX:+{0}%  MIN:-{0}%", head.CapRate.ToString()));
                    //MAX:18%
                    if (sheet.GetRow(76) == null) sheet.CreateRow(76);
                    if (sheet.GetRow(76).GetCell(4) == null) sheet.GetRow(76).CreateCell(4);
                    sheet.GetRow(76).GetCell(4).SetCellValue(
                        string.Format("less than:{0}", head.DfMaxAfter));
                    //MAX:352uA
                    if (sheet.GetRow(76) == null) sheet.CreateRow(76);
                    if (sheet.GetRow(76).GetCell(5) == null) sheet.GetRow(76).CreateCell(5);
                    sheet.GetRow(76).GetCell(5).SetCellValue(
                        string.Format("less than:{0}uA", head.LcMax.ToString()));

                    //Tested by:王燕
                    sheet.GetRow(85).GetCell(headColumnsIndex + 27).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }


                //headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 9;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 6;
            int dtlColumns = 8;
            int dtlRowsCount = 17;

            string RT = "RT:";
            string RH = "RH:";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i >= 10) break;

                try
                {
                    if (i >0)
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex+1).SetCellValue(
                            head.Dtls[i].TestTime.ToString() + "hr");

                    if (i==0 || i==5)
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 4).SetCellValue(
                            head.Dtls[i].TestDate.ToString("yyyy/M/d"));
                    else
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 4).SetCellValue(
                            head.Dtls[i].TestDate.ToString("yyyy/M/d"));

                    //RT: 
                    //sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex).SetCellValue(RT);
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 1).SetCellValue(
                        head.Dtls[i].GetTemp());
                    //sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 2).SetCellValue(RH);
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 4).SetCellValue(
                        head.Dtls[i].GetHumidity());

                    sheet.GetRow(currentHeadRowIndex + 16).GetCell(currentDtlColumnIndex).SetCellValue(
                        head.Dtls[i].Remark);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");

                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(dataRowIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = carpss.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (carpss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                if(i==0)cIndex++;
                else cIndex += 2;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value,2));
                    }
                }

                cIndex++;
                var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = lcss.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                lcs = null;
                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "ESR");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = lcss.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                lcs = null;
                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = lcss.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }


                cIndex++;
                lcs = null;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "L");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = lcss.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }
                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;

                if (i==4)
                {
                    currentDtlColumnIndex = 1;
                    currentDataColumnIndex = 1;
                    currentHeadRowIndex =23;
                    dataRowIndex =26;
                }
            }

            #endregion

            //int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            //int firstDataRow = 5;
            //if (headCount < 5)
            //{
            //    int lastDataRowIndex = firstDataRow + dtlRowsCount * 5;
            //    int firstHideRowIndex = firstDataRow + (headCount > 1 ? headCount : 2) * dtlRowsCount;
            //    for (int i = firstHideRowIndex; i < lastDataRowIndex - 1; i++)
            //    {
            //        sheet.GetRow(i).ZeroHeight = true;
            //    }
            //}

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
